<!-- 调节协议 -->
<template>
	<a-card :bordered="false">
		<a-form border="false" ref="searchFormRef" :model="searchFormState">
			<a-row :gutter="24">
				<a-col :span="5">
					<a-form-item label="调解中心" name="configId">
						<a-select v-model:value="searchFormState.configId" placeholder="请选择所属调解中心">
							<a-select-option v-for="item in configArr" :key="item.id" :value="item.id"
								>{{ item.name }}
							</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-form-item name="debtorCardId" label="身份证号">
						<a-input
							v-model:value="searchFormState.debtorCardId"
							placeholder="请输入身份证号"
							enter-button
							allowClear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-form-item name="liaisonPhone" label="手机号">
						<a-input v-model:value="searchFormState.liaisonPhone" placeholder="请输入手机号" enter-button allowClear />
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-form-item label="回传时间">
						<a-range-picker v-model:value="createTimeArr" @change="dateChange" />
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false" class="mt-2">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:scroll="{ x: 'max-content' }"
		>
			<template #operator>
				<a-space>
					<a-button type="primary" @click="exportExcel">
						<download-outlined />
						导出
					</a-button>
				</a-space>
			</template>
		</s-table>
	</a-card>
</template>

<script setup name="accessLog">
	import accessLogApi from '@/api/website/access/accessLogApi'
	import dayjs from 'dayjs'
	import downloadUtil from '@/utils/downloadUtil'
	import configApi from '@/api/website/config/configApi'

	const searchFormRef = ref()
	const searchFormState = ref({
		debtorCardId: undefined,
		liaisonPhone: undefined,
		beginTime: dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
		endTime: dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss')
	})
	const tableRef = ref()
	const createTimeArr = ref([dayjs().startOf('day'), dayjs().endOf('day')])
	const configArr = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: false, striped: false }
	const columns = [
		{
			title: '身份证号',
			dataIndex: 'debtorCardId',
			align: 'center'
		},
		{
			title: '手机号',
			dataIndex: 'liaisonPhone',
			align: 'center'
		},
		{
			title: '渠道',
			dataIndex: 'channel',
			align: 'center'
		},
		{
			title: '访问IP',
			dataIndex: 'ip',
			align: 'center'
		},
		{
			title: '访问时间',
			dataIndex: 'createTime',
			align: 'center'
		}
	]

	const dateChange = (value) => {
		searchFormState.value.beginTime = value[0].format('YYYY-MM-DD').concat(' 00:00:00')
		searchFormState.value.endTime = value[1].format('YYYY-MM-DD').concat(' 23:59:59')
	}

	const loadData = (parameter) => {
		searchFormState.value.current = parameter.current
		searchFormState.value.size = parameter.size
		return accessLogApi.pageList(searchFormState.value).then((res) => {
			return res
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		createTimeArr.value = [dayjs().startOf('day'), dayjs().endOf('day')]
		searchFormState.value.beginTime = dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss')
		searchFormState.value.endTime = dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss')
		tableRef.value.refresh(true)
	}

	const exportExcel = () => {
		accessLogApi.exportExcel(searchFormState.value).then((res) => {
			downloadUtil.resultDownload(res)
		})
	}
	onMounted(() => {
		configApi.getCompanyDict().then((res) => {
			configArr.value = Object.entries(res).map(([id, name]) => ({
				id: parseInt(id),
				name: name
			}))
		})
	})
</script>
